package com.SlideMenu.chatlove;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.kdstudio.network.URLCachePolicy;
import com.kdstudio.network.URLRequest;
import com.kdstudio.network.URLRequestDelegate;
import com.kdstudio.network.reponse.URLJSONReponse;
import com.vg.Adapter.adapter_Friend;
import com.vg.Adapter.adapter_yahoo;
import com.vg.classSample.Friend;
import com.vg.classSample.yahoo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ListView;

public class lv_Yahoo extends Activity {

	String url = "http://android.dzanchoi.vn/api/images/list.xmob?id=17";
	ListView lv;
	ArrayList<yahoo> arr;
	adapter_yahoo adapter;
	private Handler mhHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview_ya);
		lv=(ListView)findViewById(R.id.listview);
		arr=new ArrayList<yahoo>();
		mhHandler = new Handler();

		URLRequest request = new URLRequest(url, new RequestDelegate());
		 request.setReponse(new URLJSONReponse());
		request.setCachePolicy(URLCachePolicy.DISK);
		request.send();
	}

	private class RequestDelegate extends URLRequestDelegate {
		@Override
		public void onRequestReponse(final URLRequest httpRequest) {
			mhHandler.post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					// URLBitmapResponse
					// reponse=(URLBitmapResponse)httpRequest.getReponse();
					// img.setImageBitmap(reponse.bitmap);

					URLJSONReponse response = (URLJSONReponse) httpRequest
							.getReponse();
					
					JSONArray jarr;
					try {
						jarr = response.jsonObj.getJSONArray("data");
						for (int i = 0; i < 20; i++) {
							JSONObject obj = jarr.getJSONObject(i);
								String name_test = obj.getString("name");
								String Status_test = obj.getString("body");
								String img = obj.getString("images");
							yahoo yahoo = new yahoo(Status_test,name_test,img);
							arr.add(yahoo);
							adapter = new adapter_yahoo(getApplicationContext(), arr);
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
					lv.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					// tv.setText(response.jsonObj.toString());

				}
			});

		}

		// anh hien thi khi timeout
		@Override
		public void onRequestError(URLRequest httpRequest, Throwable error) {
			// TODO Auto-generated method stub

		}

		// chay tu dau la anh gi khi load xong se thay doi
		@Override
		public void onRequestStart(URLRequest httpRequest) {
			// TODO Auto-generated method stub
			super.onRequestStart(httpRequest);
		}
	}
}
